#!/bin/bash 
set -e

TMP=${TMP:-/tmp/work}
LOG=${LOG:-$HOME/logs/stage3}
SRC=${SRC:-/sources}

name=coreutils
version=8.32

rm -rf $TMP
mkdir -p $TMP $LOG

tar xf $SRC/$name-$version.tar.xz -C $TMP

{ time \
   {

    cd $TMP/$name-$version
    
    patch -Np1 -i $SRC/$name-$version-i18n-1.patch
    
    sed -i '/test.lock/s/^/#/' gnulib-tests/gnulib.mk
	
	echo '# deleted' > m4/std-gnu11.m4
    
	autoreconf -fiv
	FORCE_UNSAFE_CONFIGURE=1 ./configure \
	            --prefix=/usr            \
	            --enable-no-install-program=kill,uptime
    
    make
    
    make install
    
	mv -v /usr/bin/{cat,chgrp,chmod,chown,cp,date,dd,df,echo} /bin
	mv -v /usr/bin/{false,ln,ls,mkdir,mknod,mv,pwd,rm} /bin
	mv -v /usr/bin/{rmdir,stty,sync,true,uname} /bin
	mv -v /usr/bin/chroot /usr/sbin
	mv -v /usr/share/man/man1/chroot.1 /usr/share/man/man8/chroot.8
	sed -i 's/"1"/"8"/' /usr/share/man/man8/chroot.8

	mv -v /usr/bin/{head,nice,sleep,touch} /bin

    }
} 2>&1 | tee $name.log
    
[ $PIPESTATUS = 0 ] && mv $name.log $LOG || exit $PIPESTATUS

rm -fr $TMP
